package com.wande.dataplatform.controller;

import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.dto.ImpactAnalysisDTO;
import com.wande.dataplatform.domain.dto.MetadataQueryDTO;
import com.wande.dataplatform.domain.vo.DataLineageVO;
import com.wande.dataplatform.domain.vo.MetadataVO;
import com.wande.dataplatform.service.IMetadataService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 元数据管理控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/metadata")
@RequiredArgsConstructor
public class MetadataController extends BaseController {

    private final IMetadataService metadataService;

    /**
     * 查询元数据列表
     */
    @GetMapping("/list")
    public TableDataInfo<MetadataVO> list(MetadataQueryDTO queryDTO, PageQuery pageQuery) {
        return metadataService.queryMetadataList(queryDTO, pageQuery);
    }

    /**
     * 查询元数据详情
     */
    @GetMapping("/{id}")
    public R<MetadataVO> getInfo(@PathVariable Long id) {
        return R.ok(metadataService.getMetadataById(id));
    }

    /**
     * 采集数据源元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/collect/{datasourceId}")
    public R<Integer> collect(@PathVariable Long datasourceId) {
        Integer count = metadataService.collectMetadata(datasourceId);
        return R.ok("采集成功，共采集 " + count + " 个表", count);
    }

    /**
     * 采集指定表的元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/collect/table")
    public R<Long> collectTable(@RequestParam Long datasourceId,
                                @RequestParam String databaseName,
                                @RequestParam String tableName) {
        Long metadataId = metadataService.collectTableMetadata(datasourceId, databaseName, tableName);
        return R.ok("采集成功", metadataId);
    }

    /**
     * 搜索元数据
     */
    @GetMapping("/search")
    public TableDataInfo<MetadataVO> search(@RequestParam String keyword, PageQuery pageQuery) {
        return metadataService.searchMetadata(keyword, pageQuery);
    }

    /**
     * 导出元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public R<List<MetadataVO>> export(@RequestBody MetadataQueryDTO queryDTO) {
        List<MetadataVO> list = metadataService.exportMetadata(queryDTO);
        return R.ok(list);
    }

    /**
     * 导入元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Integer> importData(@RequestBody List<MetadataVO> metadataList) {
        Integer count = metadataService.importMetadata(metadataList);
        return R.ok("导入成功，共导入 " + count + " 条记录", count);
    }

    /**
     * 删除元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        metadataService.deleteMetadata(id);
        return R.ok();
    }

    /**
     * 批量删除元数据
     */
    @Log(title = "元数据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch")
    public R<Void> batchRemove(@RequestBody List<Long> ids) {
        metadataService.batchDeleteMetadata(ids);
        return R.ok();
    }

    /**
     * 构建数据血缘
     */
    @Log(title = "元数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/lineage/build")
    public R<Void> buildLineage(@RequestParam Long taskId, @RequestParam String taskType) {
        metadataService.buildLineage(taskId, taskType);
        return R.ok();
    }

    /**
     * 查询上游依赖
     */
    @GetMapping("/lineage/upstream/{tableId}")
    public R<List<DataLineageVO>> getUpstream(@PathVariable Long tableId) {
        return R.ok(metadataService.getUpstream(tableId));
    }

    /**
     * 查询下游影响
     */
    @GetMapping("/lineage/downstream/{tableId}")
    public R<List<DataLineageVO>> getDownstream(@PathVariable Long tableId) {
        return R.ok(metadataService.getDownstream(tableId));
    }

    /**
     * 影响分析
     */
    @GetMapping("/impact/analyze")
    public R<ImpactAnalysisDTO> analyzeImpact(@RequestParam Long tableId, 
                                              @RequestParam String changeType) {
        return R.ok(metadataService.analyzeImpact(tableId, changeType));
    }

    /**
     * 查询血缘关系图
     */
    @GetMapping("/lineage/graph/{tableId}")
    public R<Object> getLineageGraph(@PathVariable Long tableId, 
                                     @RequestParam(required = false, defaultValue = "2") Integer depth) {
        return R.ok(metadataService.getLineageGraph(tableId, depth));
    }
}
